﻿Public Class Details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim AlbID As Integer = Request.QueryString("ID")
        Dim sql As String = " select Album_Name,Description, Cover_Picture from Albums Where ID =" & AlbID & ""
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)

        Try
            conn.Open()
            Dim DR As OleDb.OleDbDataReader = cmd.ExecuteReader()
            DR.Read()
            Dim albName As String = CStr(DR("Album_Name"))
            Dim Desp As String = CStr(DR("Description"))
            Dim CoverPicture As String = CStr(DR("Cover_Picture"))
            Album_Name_lbl.Text = " Album Name : " + albName
            Desc_lbl.Text = Desp
            Image1.ImageUrl = CoverPicture
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            CommentsBody()
            Dim AR As Double = rating_given(AlbID)
            If AR = 0 Then
                lbl_ratingAlbum.Text = " Rating not given By any user "
            Else
                lbl_ratingAlbum.Text = CStr(AR) + " / 5"
            End If
        Catch Ex As Exception
            Desc_lbl.Text = "************* No Data exits *************"
            Image1.Visible = False
            lbl_ratingAlbum.Visible = False
        End Try

        img_like.Visible = False
        img_unlike.Visible = False
        rbl_like.Visible = False
        btn_click.Visible = False


        Dim UserLike_Unlike As Integer = LikesUnlikes()
        Dim Record_Count As Integer = Count()

        If Record_Count = 0 Then
            lbl_error_message.Visible = True
            btn_click.Visible = True
            rbl_like.Visible = True
            img_like.Visible = False
            img_unlike.Visible = False
        ElseIf UserLike_Unlike = 1 Then
            img_like.Visible = True
            lbl_error_message.Visible = True
            lbl_error_message.Text = " I like this album!"
            img_unlike.Visible = False
            btn_click.Visible = False
            rbl_like.Visible = False
        ElseIf UserLike_Unlike = 0 Then
            img_unlike.Visible = True
            lbl_error_message.Visible = True
            lbl_error_message.Text = " Not Bad...! "
            btn_click.Visible = False
            rbl_like.Visible = False
            img_like.Visible = False
        Else
            img_like.Visible = False
            img_unlike.Visible = False
            lbl_error_message.Visible = False
            btn_click.Visible = False
            rbl_like.Visible = False
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Response.Redirect("list.aspx")
    End Sub

    Private Sub submit_btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles submit_btn.Click
        Dim AlbID As Integer = Request.QueryString("ID")
        If name_txtbox.Text = "" And comment_txtbox.Text = "" Then
            vld_UserName.Text = "Please enter username"
        Else
            Dim Uid As Integer
            Uid = CInt(InsertuserID(name_txtbox.Text))

            Dim sql As String = " INSERT INTO comments (comment_ald_ID,comment_user_ID, comments, comment_date) VALUES (@v1,@v2,@v3,@v4)"
            Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
            Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@v1", AlbID)
            cmd.Parameters.AddWithValue("@v2", Uid)
            cmd.Parameters.AddWithValue("@v3", comment_txtbox.Text)
            cmd.Parameters.AddWithValue("@v4", Date.Today)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            rating(AlbID)
            comment_txtbox.Text = String.Empty
            name_txtbox.Text = String.Empty

        End If
        Response.Redirect("Details.aspx?ID=" & AlbID)
    End Sub

    Public Sub CommentsBody()
        Dim AlbID As Integer = Request.QueryString("ID")
        Dim i As Integer
        Dim sql As String = " select comments.comments, aspnet_Users.UserName from comments, aspnet_Users Where comments.comment_ald_ID =" & AlbID & "AND comments.comment_user_ID = aspnet_Users.UserId "
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        Dim adapter As OleDb.OleDbDataAdapter
        Dim ds As New DataSet
        conn.Open()
        adapter = New OleDb.OleDbDataAdapter(sql, conn)
        adapter.Fill(ds)
        adapter.Dispose()
        conn.Close()
        For i = 0 To ds.Tables(0).Rows.Count - 1
            lbl_comment.Text += "<div style='background-color:pink'>" & ds.Tables(0).Rows(i).Item(0) & " --- Commented By " & ds.Tables(0).Rows(i).Item(1) & "</div><hr>"
        Next
        If ds.Tables(0).Rows.Count = 0 Then
            lbl_comment.ForeColor = Drawing.Color.DarkRed
            lbl_comment.Text = " No comments, Please post to be a first commenter for this album. "
        End If
    End Sub

    Private Sub CommentsBody(ByVal iDictionary As Collections.IDictionary)
        Throw New NotImplementedException
    End Sub
    Private Function InsertuserID(ByVal N As String) As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select aspnet_Users.UserId,aspnet_Users.UserName from aspnet_Users Where aspnet_Users.UserName = '" & N & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dim userID As Integer
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            userID = Dreader("UserId")
        Catch
            vld_UserName.Text = "Not a valid User"
        End Try
        Return userID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()


    End Function
    
    Private Sub rating(ByVal ald_id As Integer)
        Dim sql As String = " INSERT INTO Ratings (alb_ID,rating) VALUES (@r1,@r2)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@v1", ald_id)
        cmd.Parameters.AddWithValue("@v2", rbl_AlbumRating.SelectedValue)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
    End Sub
    Private Function rating_given(ByVal R As Integer) As Decimal
        Dim AvgR As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select avg(rating) as avgR from Ratings Where alb_ID = " & R & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            AvgR = CDbl(Dreader("avgR"))

            conn.Close()
            IDcmd.Dispose()
            conn.Dispose()
        Catch Ce As Exception
            AvgR = 0
        End Try
        Return AvgR
    End Function
    
    Protected Sub btn_click_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_click.Click
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim AID As Integer = Request.QueryString("ID")
        Dim sql As String = " INSERT INTO Likes (AlbumID, UserID, Likes_Unlikes) VALUES (@v1,@v2,@v3)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v1", AID)
        cmd.Parameters.AddWithValue("@v2", Uid)
        cmd.Parameters.AddWithValue("@v3", rbl_like.SelectedValue)
        conn.Open()
        Try
            If rbl_like.SelectedItem.Selected = True Then
                cmd.ExecuteNonQuery()
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            End If

        Catch

        End Try
               
        Response.Redirect("Details.aspx?ID=" & AID & "")
    End Sub
    Protected Function userID(ByVal uname As String) As Integer
        Dim uID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select UserId from aspnet_Users Where aspnet_Users.UserName = '" & uname & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            uID = Dreader("UserId")
        Catch
        End Try
        Return uID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()
    End Function
    Protected Function Count() As Integer
        Dim U_id As Integer
        Dim Uname As String = Page.User.Identity.Name
        U_id = CInt(userID(Uname))
        Dim AID As Integer = CInt(Request.QueryString("ID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select Count(*) As RowCount from Likes Where UserID =" & U_id & " AND AlbumID = " & AID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dreader = IDcmd.ExecuteReader()
        Dreader.Read()
        Dim ULike_Count As Integer = CInt(Dreader("RowCount"))
        Return ULike_Count
    End Function
    Protected Function LikesUnlikes() As Integer
        Dim U_id As Integer
        Dim Uname As String = Page.User.Identity.Name
        Dim ULike As Integer
        U_id = CInt(userID(Uname))
        Dim AID As Integer = CInt(Request.QueryString("ID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select  Likes_Unlikes from Likes Where UserID =" & U_id & " AND AlbumID = " & AID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            ULike = CInt(Dreader("Likes_Unlikes"))
        Catch
        End Try
        Return ULike
    End Function
End Class